package com.seek.service.imp;


import com.seek.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.seek.core.BaseServiceImpl;
import com.seek.dao.BizFlowDao;
import com.seek.model.entity.TBizFlow;
import com.seek.service.BizFlowService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository("bizFlowService")
public class BizFlowServiceImpl extends BaseServiceImpl<TBizFlow> implements BizFlowService {

  @Autowired
  public void setDao(BizFlowDao bizFlowDao) {
    super.setDao(bizFlowDao);
  }

  public BizFlowDao getDao() {
    return (BizFlowDao) super.getDao();
  }

  @Override
  public List<TBizFlow> getBizFlowByParams(Map<String, Object> paramsMap) {

    List<Object> queryParams = new ArrayList<>();

    List<TBizFlow> result = new ArrayList<>();
    try {
      StringBuffer sql = new StringBuffer(
          "select company_id as companyId,customer_id as customerId,biz_no as bizNo,biz_type as "
              + "bizType,org_code as orgCode,status,deleted from t_biz_flow where 1=1");
      if (paramsMap.containsKey("bizNo")) {
        sql.append(" and biz_no = ? ");
        queryParams.add(paramsMap.get("bizNo"));
      }
      if (paramsMap.containsKey("orgBizNo")) {
        sql.append(" and org_biz_no = ? ");
        queryParams.add(paramsMap.get("orgBizNo"));
      }


      if (paramsMap.containsKey("deleted")) {
        sql.append(" and deleted = ? ");
        queryParams.add(paramsMap.get("deleted"));
      }

      sql.append(" order by create_time desc LIMIT 1  ");
      List<Map<String, Object>> queryResult = this.getDao()
          .findMapBySql(sql.toString(), queryParams.toArray(new Object[queryParams.size()]));
      if (CollectionUtils.isNotEmpty(queryResult)) {

        for (Map obj : queryResult) {
          TBizFlow bizFlow = new TBizFlow();
          bizFlow.setCompanyId(CommonUtil.getLongValue(obj.get("companyId")));
          bizFlow.setCustomerId(CommonUtil.getLongValue(obj.get("customerId")));
          bizFlow.setBizNo(String.valueOf(obj.get("bizNo")));
          bizFlow.setBizType(String.valueOf(obj.get("bizType")));
          bizFlow.setOrgCode(String.valueOf(obj.get("orgCode")));
          bizFlow.setStatus(String.valueOf(obj.get("status")));
          bizFlow.setDeleted(CommonUtil.getIntValue(obj.get("deleted")));
          result.add(bizFlow);
        }
      }
      return result;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void updateBizFlow(String sql, Object... params) throws Exception{
    this.getDao().executeUpdateSql(sql,params);
  }

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void addBizFlow(TBizFlow bizFlow) throws Exception {
	this.getDao().saveOrUpdate(bizFlow);
	
}
}